﻿using Hotel.Domain;
using Hotel.IRepository;
using Hotel.IRepository.Migrations;
using Hotel.IServices;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.Repository
{
    public class RBACRepository: IRBACRepository
    {

        private readonly HotelDBContext ctx;
        private readonly ILogger<RBACRepository>  logger;

        public RBACRepository(HotelDBContext ctx, ILogger<RBACRepository> logger)
        {
            this.ctx = ctx;
            this.logger = logger;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="users">用户表</param>
        /// <param name="list">用户角色表</param>
        /// <returns></returns>
        public async Task<bool> RegisterAsync(User users,List<UserRoleInfo> list)
        {
            //开启事务
            using (var tran = ctx.Database.BeginTransaction()) {
                try
                {
                    //添加用户
                  await  ctx.Users.AddAsync(users);
                    //保存 到数据库
                    await ctx.SaveChangesAsync();
                    var userid = users.UserId;
                    foreach (var item in list)
                    {
                        item.UserId = userid;
                    }
                    //添加用户角色
                    await ctx.UserRoleInfos.AddRangeAsync(list);

                    //保存 到数据库
                    await  ctx.SaveChangesAsync();


                    //提交事务
                    await tran.CommitAsync();
                  return true;
                }
                catch (Exception)
                {
                    //回滚事务
                    await tran.RollbackAsync();
                    throw;
                }
            }

        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="users">用户表</param>
        /// <param name="list">用户角色表</param>
        /// <returns></returns>
        public async Task<bool> UpDateUserInfoAsync(User users, List<UserRoleInfo> list)
        {
            //开启事务
            using (var tran = ctx.Database.BeginTransaction())
            {
                try
                {
                    //添加用户
                     ctx.Users.Update(users);
                 

                    //查询已经有的角色
                  var userrole=  ctx.UserRoleInfos.Where(p => p.UserId == users.UserId).ToList();
                    //删除已经存在的用户角色
                    ctx.UserRoleInfos.RemoveRange(userrole);
                    //重新添加用户角色
                    await ctx.UserRoleInfos.AddRangeAsync(list);

                    //保存 到数据库
                    await ctx.SaveChangesAsync();


                    //提交事务
                    await tran.CommitAsync();
                    return true;
                }
                catch (Exception)
                {
                    //回滚事务
                    await tran.RollbackAsync();
                    throw;
                }
            }

        }


        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<bool> AddRoleAsync(RoleInfo role, List<RolePermisstionInfo> list)
        {
            using (var tran=ctx.Database.BeginTransaction())
            {
                try
                {
                    await ctx.RoleInfos.AddAsync(role);
                         //保存到数据库
                    await ctx.SaveChangesAsync();
                  //  var roleid = role.RoleId;
                    foreach (var item in list)
                    {
                        item.RoleId = role.RoleId;
                    }


                    //添加角色权限信息
                    await ctx.RolePermisstionInfos.AddRangeAsync(list);
                    //保存到数据库
                    await ctx.SaveChangesAsync();
                    //提交事务
                    tran.Commit();
                    return true;
                }
                catch (Exception)
                {

                    //回滚事务
                    tran.Rollback();
                    throw;
                }
            }
        }


        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<bool> UpDataRoleAsync(RoleInfo role, List<RolePermisstionInfo> list)
        {
            using (var tran = ctx.Database.BeginTransaction())
            {
                try
                {
                    //修改角色信息
                     ctx.RoleInfos.Update(role);
                   
                    //查询已有的角色权限
                    var rolepermission=ctx.RolePermisstionInfos.Where(p=>p.RoleId==role.RoleId).ToList();

                    //删除已有的角色权限
                    ctx.RolePermisstionInfos.RemoveRange(rolepermission);

                    //添加角色权限信息
                    await ctx.RolePermisstionInfos.AddRangeAsync(list);
                    //保存到数据库
                    await ctx.SaveChangesAsync();
                    //提交事务
                    tran.Commit();
                    return true;
                }
                catch (Exception)
                {

                    //回滚事务
                    tran.Rollback();
                    throw;
                }
            }
        }
    }
}
